var t = getApp(),
  core = t.requirejs("core"),
  e = t.requirejs("check");
Page({
  data: {
    route: "product",
    menu: t.tabBar,
    menu_show: false,
    icons: t.requirejs("icons"),
    page: 1,
    loaded:false,
    list: [],
    dikou_price:"",
    cate_id: "",
    cate_id1: "",
    cate_index: "",
    cate_id2: "",
    cate_id3: "",
    category:[],
    cate2:[],
    cate3:[],
    saff_id:0,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    navData: [
      {
        text: '首页'
      },
      {
        text: '健康'
      },
      {
        text: '情感'
      },
      {
        text: '职场'
      },
      {
        text: '育儿'
      },
      {
        text: '纠纷'
      },
      {
        text: '青葱'
      },
      {
        text: '上课'
      },
      {
        text: '下课'
      }
    ]
  },
  //底部导航跳转
  menu_url: function (k) {
    core.menu_url(k, 2);
  },
  onLoad: function (t) {
    console.log('saff_id',t)
    var cate_id = t.id || '';
    var saff_id = t.saff_id || 0;
    this.setData({
      cate_id: cate_id,
      saff_id: saff_id
    });  
    this.initCategory();
    this.getList();
    core.setting();
  },
  onShow: function () {
    
  },
  /**
  * 下拉刷新
  */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      loaded: false,
      list: []
    })
    this.initCategory();
    this.getList();
    wx.stopPullDownRefresh();
  },
  /**
   *上拉加载
   */
  onReachBottom: function () {
    this.data.loaded || this.getList()
  },

  /**
   * 获取商品分类
   * @return array
   */
  initCategory: function () {
    var t = this;
    core.get("goods/GetCate", {}, function (a) {
      console.log("分类",a);
      t.setData({
        category: a.info,
      })
    })
  },

  /**
   * 获取商品列表
   * @return array
   */
  getList: function () {
    console.log("current_cate_id",this.data.cate_id);
    var t = this;
      core.get("goods/GoodsList", {
        page:t.data.page,
        cate_id: t.data.cate_id,
        saff_id: t.data.saff_id
      }, function (a) {
        console.log(a)
        // var dikou_price =a.data.dikou_price;
        // t.setData({
        //   dikou_price =a.data.dikou_price
        // })
        if (a.code == 0) {
          if (a.info.length == 0) {
            t.setData({
              loaded:true
            })
            return false;
          }
          var e = {};
          a.info.length > 0 && (e.page = t.data.page + 1, e.list = t.data.list.concat(a.info), a.info.length < 10 && (e.loaded = true)),
            t.setData(e)
        } else {
          core.alert(a.msg)
        }

      },true)

  },

  // 页面已注释，暂时不用
  selected: function(t){
    var that=this,
    index = core.pdata(t).index,
    cate_id=core.pdata(t).type;
    that.setData({
      list: [],
      page: 1,
      loaded:false,
      cate_id : cate_id,
      scroll_left:index*20
    })
    that.getList()
  },

  // 选择搜索分类事件
  cate_select: function (t) {
    var that = this,
        cate = that.data.category,
        data = core.pdata(t),
        e={};
    console.log("data",data)
    if (data.level== 1){
      e['cate_index'] = data.index;
      e['cate_id2']='';
      e['cate_id3'] = '';
      e.cate3=[];
      if(data.id==''){
        e.cate2 = [];
      }else{
        e.cate2 = cate[data.index].cate;
      }
    }
    if (data.level == 2) {
      e.cate3 = cate[that.data.cate_index]['cate'][data.index].cate;
    }
    e['cate_id']= data.id;
    e['cate_id' + data.level] = data.id;
    console.log("res",e);
    that.setData(e)
  },

  // 确认搜索按钮
  confirm: function () {
    this.setData({
      list: [],
      page: 1,
      loaded: false,
      isFilterShow: false,
    })
    this.getList()
  },

  // 商品分类搜索图标事件
  showFilter: function () {
    this.setData({
      isFilterShow: !this.data.isFilterShow
    })
  },

  //分享
  onShareAppMessage: function () {
    return e.onShareAppMessage()
  }
  
  
})